package com.xyhuo.leetcode.january;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 删除子串后的字符串最小长度
 * https://leetcode.cn/problems/minimum-string-length-after-removing-substrings/
 *
 * @time 2024/1/10
 */
public class MinLength {

    public int minLength(String s) {
        char[] charArray = s.toCharArray();
        Deque<Character> deque = new ArrayDeque<>();
        for (int i = 0; i < charArray.length; i++) {
            if (deque.isEmpty()) {
                deque.addLast(charArray[i]);
            } else {
                Character last = deque.getLast();
                if (last == 'A' && charArray[i] == 'B') {
                    deque.removeLast();
                } else if (last == 'C' && charArray[i] == 'D') {
                    deque.removeLast();
                } else {
                    deque.addLast(charArray[i]);
                }
            }
        }
        return deque.size();
    }
}
